package com.byt.framlib.commonwidget.finder.addresslistfinder;

import android.content.Context;
import android.util.Log;

import com.byt.framlib.base.BaseConst;
import com.byt.framlib.commonutils.ACache;
import com.byt.framlib.commonwidget.finder.entity.City;
import com.byt.framlib.commonwidget.finder.entity.County;
import com.byt.framlib.commonwidget.finder.entity.Province;
import com.byt.framlib.commonwidget.finder.entity.Street;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class DefaultAddressProvider implements AddressProvider {

    private static DefaultAddressProvider instance = null;

    private List<Province> mProvinceList = new ArrayList<>();

    private DefaultAddressProvider(Context context) {
        this.mProvinceList = getAddressProvice(context);
    }

    public static DefaultAddressProvider getInstance(Context context) {
        if (instance == null) {
            synchronized (DefaultAddressProvider.class) {
                if (instance == null) {
                    instance = new DefaultAddressProvider(context);
                }
            }
        }
        return instance;
    }

    public List<Province> getAddressProvice(Context context) {
        ArrayList<Province> data = new ArrayList<>();
        ArrayList<Province> list = (ArrayList<Province>) ACache.get(context).getAsObject(BaseConst.ADDRESS_CASHE_XMXB);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setProvinceId(i);
                for (int j = 0; j < list.get(i).getCities().size(); j++) {
                    list.get(i).getCities().get(j).setCityId(j);
                    for (int k = 0; k < list.get(i).getCities().get(j).getCounties().size(); k++) {
                        list.get(i).getCities().get(j).getCounties().get(k).setCountyId(k);
                        for (int l = 0; l < list.get(i).getCities().get(j).getCounties().get(k).getStreets().size(); l++) {
                            list.get(i).getCities().get(j).getCounties().get(k).getStreets().get(l).setStreetId(l);
                        }
                    }
                }
            }
            data.addAll(list);
        } else {
            String json = null;
            try {
                json = toString(context.getAssets().open("city.json"));
                Gson mGson = new Gson();
                list = mGson.fromJson(json, new TypeToken<List<Province>>() {
                }.getType());
            } catch (IOException e) {
                e.printStackTrace();
            }
            data.addAll(list);
        }
        return data;
    }

    public String toString(InputStream is) {
        return toString(is, "utf-8");
    }

    public String toString(InputStream is, String charset) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, charset));
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                } else {
                    sb.append(line).append("\n");
                }
            }
            reader.close();
            is.close();
        } catch (IOException e) {
            Log.e("ConvertUtils", e.getMessage());
        }
        return sb.toString();
    }

    @Override
    public void provideProvinces(final AddressReceiver<Province> addressReceiver) {
        if (mProvinceList.isEmpty()) {
            return;
        }
        addressReceiver.send(new ArrayList<>(mProvinceList));
    }

    @Override
    public void provideCitiesWith(Province province, final AddressReceiver<City> addressReceiver) {
        addressReceiver.send(new ArrayList<>(province.getCities()));
    }

    @Override
    public void provideCountiesWith(City city, final AddressReceiver<County> addressReceiver) {
        addressReceiver.send(new ArrayList<>(city.getCounties()));
    }

    @Override
    public void provideStreetsWith(County county, final AddressReceiver<Street> addressReceiver) {
        addressReceiver.send(new ArrayList<>(county.getStreets()));
    }
}